<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="患者姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入患者姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" :display="true" @changeType="changeDisplay"  @queryTable="getList"></right-toolbar>
    </el-row>
    <div v-if="display==='list'">
      <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="就诊编号" align="center" prop="code" width="150"/>
        <el-table-column label="真实姓名" align="center" prop="username" />
        <el-table-column label="性别" align="center" prop="gender" width="60" />
        <el-table-column label="年龄" align="center" prop="age" width="60"/>
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="状态" align="center" prop="state">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.wx_user_state" :value="scope.row.state"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ moment(scope.row.createTime).format('YYYY-MM-DD') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-thumb"
              @click="operation(scope.row)"
              v-hasPermi="['wx:user:processInfo']"
            >处理</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['wx:user:edit']"
            >修改</el-button>
            <!--          <el-button-->
            <!--            size="mini"-->
            <!--            type="text"-->
            <!--            icon="el-icon-delete"-->
            <!--            @click="handleDelete(scope.row)"-->
            <!--            v-hasPermi="['wx:user:remove']"-->
            <!--          >删除</el-button>-->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-else>
      <el-row :gutter="6">
        <el-col :span="8" v-for="(item, index) in userList" :key="index">
          <el-card shadow="hover">
            <div class="content-main">
              <img :src="item.avatar ? avatar : 'https://jywk.wingstimes.com/prod-api/profile/avatar/2023/07/12/blob_20230712145046A001.png'" class="user-avatar">
              <div class="content-text">
                <div class="user-name">{{ item.username }}</div>
                <div class="param-box">
                  <div class="param">
                    <div class="label">患者编号</div>
                    <div class="value">{{item.code}}</div>
                  </div>
                  <div class="param">
                    <div class="label">患者手机号</div>
                    <div class="value">{{item.phone}}</div>
                  </div>
                </div>
              </div>
            </div>
          </el-card>
          <div class="button-group">
            <button :disabled="checkPermi(['wx:user:edit'])===true?false:true" @click="handleView(item)"><i class="el-icon-view"></i>查看</button>
            <button :disabled="checkPermi(['wx:user:edit'])===true?false:true"  @click="handleUpdate(item)"><i class="el-icon-edit"></i>修改</button>
            <button :disabled="checkPermi(['wx:user:processInfo'])===true?false:true"  @click="operation(item)"><i class="el-icon-thumb"></i>处理</button>
          </div>
        </el-col>
      </el-row>

    </div>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :page-sizes="pageSizes"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="患者姓名" prop="username">
              <el-input v-model="form.username" :disabled="disabled" placeholder="请输入患者姓名" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="form.phone" :disabled="disabled" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age" :disabled="disabled" placeholder="请输入年龄" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select v-model="form.gender" :disabled="disabled" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.label"
                  :label="dict.label"
                  :value="dict.label"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password">
              <el-input v-model="form.password" :disabled="disabled" placeholder="请输入用户密码" type="password" maxlength="20" show-password/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="身份证">
              <el-input v-model="form.identityCard" :disabled="disabled"  placeholder="请输入身份证号码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地址">
              <el-input v-model="form.address" :disabled="disabled" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" :disabled="disabled" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="disabled===true?false:true" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listUser, getUser, delUser, addUser, updateUser } from "@/api/wechat/user";

    export default {
        name: "unprocessedUser",
        dicts: ['wx_user_state','sys_user_sex'],
        data() {
            return {
                pageSizes:[12,24,36],
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 字典表格数据
                userList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 日期范围
                dateRange: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 12,
                    username: undefined,
                    password: undefined,
                    phone: undefined,
                    state: 2
                },
                // 密码显示隐藏
                show: true,
                //当前行
                currentIndex: null,
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    username: [
                        { required: true, message: "患者姓名不能为空", trigger: "blur" }
                    ],
                    password: [
                        { required: true, message: "密码不能为空", trigger: "blur" }
                    ]
                },
                display: 'list',
                disabled: true
            };
        },
        created() {
            this.getList();
        },
        activated() {
            this.getList();
        },
        methods: {
            /** 查询患者列表 */
            getList() {
                this.loading = true;
                listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                        this.userList = response.rows;
                        this.total = response.total;
                        this.loading = false;
                    }
                );
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.queryParams.state = 2;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 取消按钮 */
            cancel() {
                this.open = false;
                this.reset();
            },
            /** 表单重置 **/
            reset() {
                this.form = {
                    username: undefined,
                    gender: undefined,
                    age: undefined,
                    address: undefined,
                    phone: undefined,
                    identityCard: undefined,
                    remark: undefined
                };
                this.resetForm("form");
            },
            /** 查看按钮操作 */
            handleView(row) {
                console.log(row)
                this.reset();
                const userId = row.userId || this.ids
                getUser(userId).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "查看患者信息";
                    this.disabled = true;
                });
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const userId = row.userId || this.ids
                getUser(userId).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改患者基本信息";
                    this.disabled = false;
                });
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.userId != undefined) {
                            updateUser(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addUser(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 多选框选中数据 */
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.userId)
                this.single = selection.length!=1
                this.multiple = !selection.length
            },


            /** 操作 */
            operation(row){
                const userId = row.userId;
                this.$router.push("/wxchat/record/processInfo/" + userId);
            },
            changeDisplay(value){
                this.display = value
                console.log(value)
            }
        }
    };
</script>

<style lang="scss" scoped>
  .el-col{
    margin-top: 10px;
  }
  .content-main{
    display: flex;
    flex-direction: row;
    .user-avatar{
      width: 100px;
      height: 100px;
      border-radius: 15px;
      margin: 10px;
    }
    .content-text{
      display: flex;
      flex-direction: column;
      padding: 10px;
      .user-name{
        font-size: 24px;
        font-weight: bold;
      }
      .param-box{
        display: flex;
        flex-direction: row;
        margin-top: 15px;
        .param{
          margin-right: 30px;
          .label{
            margin-bottom: 10px;
            color: #5a5e66;
          }
          &:last-child {
            margin-right: 0;
          }
        }
      }
    }
  }
  .button-group{
    width: 100%;
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    button{
      width: 100%;
      height: 35px;
      font-size: 17px;
      background-color: #f6f6f6;
      border: 1px solid #e6e6e6;
      color: #409EFF;
      i{
        margin-right: 10px;
      }
    }
    button:disabled{
      pointer-events: none;
      cursor: not-allowed;
      color: #f6f6f6;
      background-color: #e6e6e6;
    }

    button:hover{
      color: #f6f6f6;
      background-color: #409EFF;

    }
  }


</style>
